/*  PrimeBlaster
    by Kjetil Eide 
    kjetile445@gmail.com

    This file is part of PrimeBlaster

    PrimeBlaster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PrimeBlaster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrimeBlaster.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.eide.spaceship;

import java.io.IOException;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.particles.ConfigurableEmitter;
import org.newdawn.slick.particles.ParticleIO;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.particles.ConfigurableEmitter.SimpleValue;
import org.newdawn.slick.geom.Vector2f;

public class EngineFlame {
	private ParticleSystem trail;
	private ConfigurableEmitter trailEmitter;
	
	private Vector2f pos;
	private int orientation;	
	
	public EngineFlame(String filename, Vector2f pos) throws SlickException {
		this.pos = pos;
		try {
			trail = ParticleIO.loadConfiguredSystem(filename);			
			//trailEmitter = ParticleIO.loadEmitter(filename);
			
			trailEmitter = (ConfigurableEmitter) trail.getEmitter(0);
		} catch (IOException e) {
			throw new SlickException("Failed to load particle systems", e);
		}
		setEnginesOff();
	}
	
	/**
	 * Make the flame point in a specific direction. The flame
	 * will point in the opposite direction of the angle specified.
	 * The angle specified should be given according to the unit cirlce.
	 * (0 degrees is 3'oclock, 90 degrees is 12'o clock etc)
	 * @param ang Angle to point flame in in degrees.
	 * 
	 */
	public void setOrientation(int ang) {
		orientation = ang;
	}
	
	public void setPos(Vector2f pos) {
		this.pos = new Vector2f(pos);
	}
		
	public void setTrailEmitter(int ang, int x, int y) {
		((SimpleValue) trailEmitter.angularOffset).setValue(ang-90);
		trailEmitter.setPosition(x, y);
	}
		
	public void setEnginesOn() {
		trailEmitter.setEnabled(true);
	}
	
	public void setEnginesOff() {
		trailEmitter.setEnabled(false);
	}
	
	public void update(int delta) {
		trail.update(delta);
	}
	
	public Vector2f getPos() {
		return pos;
	}
	
	public int getOrientation() {
		return orientation;
	}
	
	public ParticleSystem getParticleSystem() {
		return trail;
	}	
}
